Completed
Push — master ( f1da37...c68c1b )
by Mark
15s queued 11s
created

fromModel.ts ➔ arrayToObjects   C

Complexity

Conditions 11

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 5
dl 0
loc 5
rs 5.4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like fromModel.ts ➔ arrayToObjects often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import Field from "../../Store/Model/Field";
2
import Relation from "../../Store/Model/Relation";
3
4
export default function (model, fromRelation = false) {
5
    const json = {};
6
7
    for (let i = 0; i < model.originalFields.length; i++) {
8
        const field = model.originalFields[i];
9
        if (field instanceof Relation && fromRelation) {
10
            continue
11
        }
12
        json[field.name] = field?.value;
13
        if (Array.isArray(json[field.name])) {
14
            json[field.name] = arrayToObjects(json, field);
15
        }
16
    }
17
18
    return {...json};
19
}
20
21
function arrayToObjects(json: object, field: Field): Array<unknown> {
22
    return [...json[field.name].map((value) => {
23
        return (value?.toObject(true) ?? value)
24
    }) ?? []];
25
}